package flutter.plugins.screen.screen;

import android.app.Activity;
import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;


public class CallintInUIActivity extends Activity {

  private Context mContext;
  private BroadcastReceiver broadcastReceiver;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Window window = this.getWindow();
    window.addFlags(
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED //锁屏状态下显示
            | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD //解锁
            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON //保持屏幕长亮
            | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON //打开屏幕
    );
    mContext = this;
    setContentView(R.layout.calling_in_ui);
    initView();
    createBroadcastReceiver();
  }

  private void initView() {
    Intent intent = getIntent();
    String name = intent.getStringExtra("name");
    String tips = intent.getStringExtra("tips");
    TextView nameTxt = findViewById(R.id.name);
    nameTxt.setText(name);

    TextView tipTxt = findViewById(R.id.tips);
    tipTxt.setText(tips);

    findViewById(R.id.reject).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        //先解锁系统自带锁屏服务，放在锁屏界面里面
        KeyguardManager keyguardManager = (KeyguardManager)mContext.getSystemService(Context.KEYGUARD_SERVICE);
        keyguardManager.newKeyguardLock("unlock").disableKeyguard(); //解锁
        reject();
        finish();
      }
    });

    findViewById(R.id.accept).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        //先解锁系统自带锁屏服务，放在锁屏界面里面
        KeyguardManager keyguardManager = (KeyguardManager)mContext.getSystemService(Context.KEYGUARD_SERVICE);
        keyguardManager.newKeyguardLock("unlock").disableKeyguard(); //解锁
        accept();
        finish();
      }
    });
  }

  private void createBroadcastReceiver() {
    broadcastReceiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
        if(intent.getAction() == CallingInReceiver.ACTION_CALLINGINUI_FINISH) {
          finish();
        }
      }
    };
    mContext.registerReceiver(broadcastReceiver, new IntentFilter(CallingInReceiver.ACTION_CALLINGINUI_FINISH));
  }

  private void reject() {
    Intent intent = new Intent();
    intent.setAction(CallingInReceiver.ACTION_CALL_STATE_CHANGE);
    intent.putExtra("state", "reject");
    this.getApplicationContext().sendBroadcast(intent);
  }

  private  void accept() {
    Intent intent = new Intent();
    intent.setAction(CallingInReceiver.ACTION_CALL_STATE_CHANGE);
    intent.putExtra("state", "accept");
    this.getApplicationContext().sendBroadcast(intent);
  }

  @Override
  protected void onDestroy() {
    if(broadcastReceiver != null) mContext.unregisterReceiver(broadcastReceiver);
    super.onDestroy();
  }
}
